/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.web.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.ipo.mm.web.util.BreadCrumbs;
import org.ipo.mm.web.util.ConstantManager;
import org.ipo.mm.web.util.pager.AbstractPager;
import org.ipo.mm.web.util.pager.ActionsPager;
import org.ipo.mm.web.util.pager.FinishedActionsPager;
import org.ipo.mm.web.util.pager.PendingActionsPager;

/**
 *
 * @author makcro
 */
public class ActionsSearchAction extends org.apache.struts.action.Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        ActionForward forward = null;
        AbstractPager pager = null;

        BreadCrumbs bc = (BreadCrumbs) request.getSession().getAttribute(ConstantManager.SESSION_BREADCRUMB_PARAM_NAME);
        bc.push(ConstantManager.BC_ACTOINS_LIST, request);

        String actionStatusParam = request.getParameter("status");
        String pageParam = request.getParameter("p");

        // if user clicked on a status link that meens that new pager should be initialized regarding to action status
        if (actionStatusParam != null) {
            if ("finished".equals(actionStatusParam)) {
                pager = new FinishedActionsPager();
            } else if ("pending".equals(actionStatusParam)) {
                pager = new PendingActionsPager();
            }

            // and put into seasion
            request.getSession().setAttribute(ConstantManager.SESSION_ACTIONS_PAGER, pager);
        }

        // if pageparam is not null that meens that user wants to go to next page and pager from seasion should be used whatever it is
        else if(pageParam != null){
            pager = (AbstractPager) request.getSession().getAttribute(ConstantManager.SESSION_ACTIONS_PAGER);

            // and set it page number
            pager.setPageNumber(Integer.parseInt(pageParam));
        }

        // if pagenumber is null and status is null then create default actions pager and put it in the seastion
        // this meens that user opend actons page for the first time
        else {
            pager = new ActionsPager();
            request.getSession().setAttribute(ConstantManager.SESSION_ACTIONS_PAGER, pager);
        }


        pager.doPaging(request);

        forward = mapping.findForward(ConstantManager.NAVIGATION_ACTIONS_LIST);

        return forward;


    }
}
